//
//  SendLogicHandle.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/4/28.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "SendLogicHandle.h"
#import "MantisGCDTimer.h"
#import "ChatHandleCommonLogic.h"
#import "CommonHeader.h"

@implementation SendLogicHandle
static SendLogicHandle*manager=nil;
static dispatch_once_t onceToken;
+(SendLogicHandle*)keySendLoginHandle{
    dispatch_once(&onceToken, ^{
        manager=[self new];
    });
    return manager;
}


//仅仅给后台发送消息
-(void)sendChatMesage:(TIMMessage *)msg sucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock{

    TIMConversation*conversation= [[TIMManager sharedInstance]getConversation:TIM_C2C receiver:@"sdkmanager"];
    
    [conversation sendMessage:msg succ:^(){
        NSLog(@"SendMsg Succ");
        !sucessBlock?:sucessBlock();
        
    }fail:^(int code, NSString * err) {
        NSLog(@"SendMsg Failed:%d->%@", code, err);
        !failBlock?:failBlock(code,err);
       
        
    }];
}


-(void)sendPreReadNewssucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock inputStr:(NSString*)inputStr{


        //发送预读消息
        MantisTextCellData*cellData= [SendComLogicHandle sendPreReadMessageWith:inputStr chatModel:self.buildModel myModel:self.myModel];

        
         cellData.sendTime=[[NSString jk_UUIDTimestamp]doubleValue];
         [self sendChatMesage:cellData.innerMessage sucess:sucessBlock fail:failBlock];
 
}

-(void)sendOFFLINENewssucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock{
    
     MantisTextCellData*cellData=  [SendComLogicHandle sendDifferentMessaeWith:SDK_CHAT_OFFLINE chatModel:self.buildModel myModel:self.myModel contentType:@"text"];
     cellData.sendTime=[[NSString jk_UUIDTimestamp]doubleValue];
      [self sendChatMesage:cellData.innerMessage sucess:sucessBlock fail:failBlock];
}


-(void)sendTransMesagesucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock{
    
    MantisTextCellData*cellData=  [SendComLogicHandle sendDifferentMessaeWith:ON_TRANSFER chatModel:self.buildModel myModel:self.myModel contentType:@"text"];
    cellData.sendTime=[[NSString jk_UUIDTimestamp]doubleValue];
    [self sendChatMesage:cellData.innerMessage sucess:sucessBlock fail:failBlock];
    
}
-(void)sendConsultLeaveMesagesucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock{
    
    MantisTextCellData*cellData=  [SendComLogicHandle sendDifferentMessaeWith:ON_AGENT_LEAVE chatModel:self.buildModel myModel:self.myModel contentType:@"text"];
    cellData.sendTime=[[NSString jk_UUIDTimestamp]doubleValue];
    [self sendChatMesage:cellData.innerMessage sucess:sucessBlock fail:failBlock];
    
}

-(void)sendWelcomTextMesagesucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock{
    MantisTextCellData*cellData=  [SendComLogicHandle sendDifferentMessaeWith:SDK_WELCOME_MSG chatModel:self.buildModel myModel:self.myModel contentType:@"text"];
    cellData.sendTime=[[NSString jk_UUIDTimestamp]doubleValue];
     [self sendChatMesage:cellData.innerMessage sucess:sucessBlock fail:failBlock];
}


-(void)sendDifferentMessaeWith:(NSString*)sendType chatModel:(WelcomeJsonData*)chatModel myModel:(MatinsIMinitModel*)myModel contentType:(NSString*)contentType msg:(NSString*)msg sucess:(MartinImSucc)sucessBlock fail:(MartinsImFail)failBlock{
    //组装外层
    MantisCellJosonData*offlineModel = [SendComLogicHandle sendMessagePrepareWithCellJosnData:sendType by:vendor1Str];
    //组装内层content
    MantisTextCellConetentData*contentModel=[SendComLogicHandle getCellContenModelFromBuildModel:chatModel proModel:myModel type:contentType];
    contentModel.msg=msg;
    offlineModel.content=contentModel;
    

    TIMMessage*innerMessage = [[TIMMessage alloc] init];
    TIMCustomElem * custom_elem = [ChatHandleCommonLogic getCustomElemModel:offlineModel.mj_JSONData];
    [innerMessage addElem:custom_elem];
    
   [self sendChatMesage:innerMessage sucess:sucessBlock fail:failBlock];
}






-(void)clearAllLogic{
    onceToken=0;
    self.buildModel=nil;
    self.myModel=nil;
}



@end
